<?php
class AjaxController extends Zend_Controller_Action
{
	public function init()
    {
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender();
    }
    
    private function _reqmodel()
    {
		$inputs = $this->_getAllParams();
		$model = 'Application_Model_'. $inputs['md'];
		$function = $inputs['fn'];
		$md = new $model;  
		return $rsp = $md->$function($inputs);
    }
    
    public function authAction()
    {
        ////檢查他是否具有管理員權限？
    	if($this->getRequest()->isPost() && Administrator_Model_admin::isLogin())
    	{
		    $rsp = $this->_reqmodel();
		    echo Zend_Json::encode($rsp);
    	}
    	else 
    	{
    		echo Zend_Json::encode(NULL);
    	}
    }
    
	public function postAction()
    {
    	if($this->getRequest()->isPost())
    	{
			$rsp = $this->_reqmodel();
		    echo Zend_Json::encode($rsp);
    	}
    	else 
    	{
    		echo Zend_Json::encode(NULL);
    	}
    }
    
    public function storegetAction()
    {
    	$ary = $this->_reqmodel();
    	
		$rsp['success'] = true;
        $rsp["total"] = count($ary);
        $rsp['rows'] = $ary;
    	
	    echo Zend_Json::encode($rsp);
    }
    
	public function getAction()
    {
    	$rsp = $this->_reqmodel();
	    echo Zend_Json::encode($rsp);
    }
    
    public function paginatorAction()
    {
    	$inputs = $this->_getAllParams();
//    	$table = 'Administrator_Model_DbTable_'. $inputs['tb'];
$table = $model = 'Administrator_Model_'. $inputs['md'];
	    $func = $inputs['fn'];
	    $db = new $table;  
	    $DBSel = $db->$func($inputs);
	    
    	$currentpage = (isset($inputs['page'])) ? $inputs['page'] : 1;
        $rows = (isset($inputs['limit'])) ? $inputs['limit'] : 1;
        
        $paginator = Zend_Paginator::factory($DBSel);
        $paginator->setItemCountPerPage($rows)
        	->setCurrentPageNumber($currentpage);
        
        $rsp['success'] = true;
        $rsp["total"] = $paginator->getTotalItemCount();
        $rsp['rows'] = $paginator->getItemsByPage($currentpage)->toArray();
        
        echo Zend_Json::encode($rsp);
    }
    

    

}